package org.zhuxian.cn.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.zhuxian.cn.entity.DTO.UserLoginDTO;
import org.zhuxian.cn.entity.User;
import org.zhuxian.cn.entity.VO.UserVO;
import org.zhuxian.cn.utils.JwtUtils;
import org.zhuxian.cn.result.JsonResult;
import org.zhuxian.cn.service.UserService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理", description = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户登录", notes = "处理用户登录请求，成功后返回用户信息和认证token")
    @PostMapping("/login")
    public ResponseEntity<JsonResult> login(@ApiParam(value = "用户登录信息") @RequestBody UserLoginDTO userLogin) {
        User user = new User();
        BeanUtils.copyProperties(userLogin, user);
        if (userService.registerUser(user)) {
            // 生成 token
            String token = JwtUtils.generateToken(userLogin.getUsername());

            // 创建响应头并添加 token
            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization", "Bearer " + token);

            // 返回带有 token 的响应
            JsonResult jsonResult = new JsonResult(200, "登录成功", userService.getUserByUsername(userLogin.getUsername()));
            return new ResponseEntity<>(jsonResult, headers, HttpStatus.OK);
        }
        JsonResult jsonResult = new JsonResult(403, "用户名或密码错误，或者账号被禁用");
        return new ResponseEntity<>(jsonResult, HttpStatus.FORBIDDEN);
    }

    @ApiOperation(value = "删除用户", notes = "根据用户ID删除用户信息")
    @PostMapping("/del/{id}")
    public JsonResult deleteUser(@ApiParam(value = "用户ID") @PathVariable Long id) {
        if (userService.deleteUser(id)) {
            return new JsonResult(200, "删除成功");
        }
        return new JsonResult(400, "删除失败，用户不存在");
    }

    @ApiOperation(value = "更新用户信息", notes = "更新用户的基本信息")
    @PostMapping("/update")
    public JsonResult updateUser(@ApiParam(value = "用户信息") @RequestBody User user) {
        if (userService.updateUser(user)) {
            return new JsonResult(200, "更新成功");
        }
        return new JsonResult(400, "更新失败，用户不存在");
    }

    @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息")
    @GetMapping("/{id}")
    public JsonResult getUserById(@ApiParam(value = "用户ID") @PathVariable Long id) {
        UserVO user = userService.getUserById(id);
        if (user != null) {
            return new JsonResult(200, "获取成功", user);
        }
        return new JsonResult(404, "用户不存在");
    }

    @ApiOperation(value = "获取所有用户", notes = "获取系统中所有用户的信息")
    @GetMapping("/all")
    public JsonResult getAllUsers() {
        List<UserVO> users = userService.getAllUsers();
        return new JsonResult(200, "获取成功", users);
    }
}